package com.group1.bookmanagement.domain;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.group1.bookmanagement.dao.BaseDao;

@Component("pageBean")
public class PageBean<T> {

	@Resource(name = "baseDao")
	private BaseDao<T> dao;

	// hql语句
	private String hql;
	// hql参数
	private Object[] param;
	// 总页数
	private Integer totalPages;
	// 当前页数
	private Integer page = 1;
	// 查询结果总条数
	private Long count = 0L;
	// 每一页数据条数
	private Integer limit = 10;
	// 数据集
	private List<T> data;

	/**
	 * 查询
	 * 
	 * @param hql
	 */
	public void query() {
		if (param == null) {
			count = (Long) dao.find("select count(*) " + hql);
			data = dao.findAll(hql, (page - 1) * limit, limit);
		} else {
			count = (Long) dao.find("select count(*) " + hql, param);
			data = dao.findAll(hql, (page - 1) * limit, limit, param);
		}

		if (count % limit == 0) {
			totalPages = (int) (count / limit);
		} else {
			totalPages = (int) (count / limit) + 1;
		}
	}

	public BaseDao<T> getDao() {
		return dao;
	}

	public void setDao(BaseDao<T> dao) {
		this.dao = dao;
	}

	public String getHql() {
		return hql;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public Object[] getParam() {
		return param;
	}

	public void setParam(Object... param) {
		this.param = param;
	}

	public Integer getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(Integer totalPages) {
		this.totalPages = totalPages;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Long getCount() {
		return count;
	}

	public void setCount(Long count) {
		this.count = count;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

}
